package cn.iocoder.yudao.module.mini.service.user;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import cn.iocoder.yudao.module.mini.controller.admin.user.vo.UserCreateReqVO;
import cn.iocoder.yudao.module.mini.controller.admin.user.vo.UserExportReqVO;
import cn.iocoder.yudao.module.mini.controller.admin.user.vo.UserPageReqVO;
import cn.iocoder.yudao.module.mini.controller.admin.user.vo.UserUpdateReqVO;
import cn.iocoder.yudao.module.mini.convert.user.UserConvert;
import cn.iocoder.yudao.module.mini.dal.dataobject.user.UserDO;
import cn.iocoder.yudao.module.mini.dal.mysql.user.MiniUserMapper;
import cn.iocoder.yudao.module.mini.enums.ErrorCodeConstants;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 小程序用户 Service 实现类
 *
 * @author dzy
 */
@Service
@Validated
public class MiniUserServiceImpl implements MiniUserService {

    @Resource
    private MiniUserMapper miniUserMapper;

    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public Long createUser(UserCreateReqVO createReqVO) {
        // 插入
        UserDO user = UserConvert.INSTANCE.convert(createReqVO);
        user.setRole("customer");
        user.setStatus(0);
        user.setRegisterIp(ServletUtils.getClientIP());
        user.setPassword(passwordEncoder.encode(createReqVO.getPassword()));
        user.setAreaCode(String.join(",", createReqVO.getAreaCodeList()));
        miniUserMapper.insert(user);

        // 返回
        return user.getId();
    }

    @Override
    public void updateUser(UserUpdateReqVO updateReqVO) {
        // 校验存在
        validateUserExists(updateReqVO.getId());
        // 更新
        UserDO updateObj = UserConvert.INSTANCE.convert(updateReqVO);
        updateObj.setAreaCode(String.join(",", updateReqVO.getAreaCodeList()));
        miniUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteUser(Long id) {
        // 校验存在
        validateUserExists(id);
        // 删除
        miniUserMapper.deleteById(id);
    }

    private void validateUserExists(Long id) {
        if (miniUserMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.USER_NOT_EXISTS);
        }
    }

    @Override
    public UserDO getUser(Long id) {
        UserDO userDO = miniUserMapper.selectById(id);
        List<Long> AreaCodeList = Arrays.stream(userDO.getAreaCode().split(",")).map(Long::parseLong).collect(Collectors.toList());
        userDO.setAreaCodeList(StrUtil.isNotBlank(userDO.getAreaCode())?AreaCodeList:null);
        userDO.setAreaName(AreaUtils.getArea(Integer.parseInt(userDO.getAreaCode().split(",")[1])).getName());
        return userDO;
    }

    @Override
    public List<UserDO> getUserList(Collection<Long> ids) {
        return miniUserMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<UserDO> getUserPage(UserPageReqVO pageReqVO) {
        pageReqVO.setRole("customer");
        return miniUserMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UserDO> getUserList(UserExportReqVO exportReqVO) {
        return miniUserMapper.selectList(exportReqVO);
    }

}
